package com.myzl.web.controller.coal;

import com.myzl.coal.domain.MessageSource;
import com.myzl.coal.service.IMessageSourceService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.utils.SecurityUtils;
import com.myzl.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息源Controller
 *
 * @author myzl
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/coal/messageSource")
public class MessageSourceController extends BaseController
{
    @Autowired
    private IMessageSourceService messageSourceService;

    /**
     * 查询消息源列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageSource messageSource)
    {
        startPage();
        messageSource.getParams().put("userId", SecurityUtils.getUserId());
        List<MessageSource> list = messageSourceService.selectMessageSourceList(messageSource);
        return getDataTable(list);
    }

    /**
     * 导出消息源列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:export')")
    @Log(title = "消息源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageSource messageSource)
    {
        List<MessageSource> list = messageSourceService.selectMessageSourceList(messageSource);
        ExcelUtil<MessageSource> util = new ExcelUtil<MessageSource>(MessageSource.class);
        util.exportExcel(response, list, "消息源数据");
    }

    /**
     * 获取消息源详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(messageSourceService.selectMessageSourceById(id));
    }

    /**
     * 新增消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:add')")
    @Log(title = "消息源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageSource messageSource)
    {
        return toAjax(messageSourceService.insertMessageSource(messageSource));
    }

    /**
     * 修改消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:edit')")
    @Log(title = "消息源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessageSource messageSource)
    {
        return toAjax(messageSourceService.updateMessageSource(messageSource));
    }

    /**
     * 删除消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:remove')")
    @Log(title = "消息源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(messageSourceService.deleteMessageSourceByIds(ids));
    }

}
